package com.app.couponsharelife.ui.dialog
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.RemoteResultBean
import com.app.couponsharelife.data.remote.retrofit.RetrofitWorker
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.listener.OnClickListener
import com.app.couponsharelife.utils.AppUtils
import com.app.couponsharelife.utils.ToastUtil
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.dialog_redpacket.*
import java.util.HashMap

/**
 *Created by xct on 2019/2/16
 *Description:红包弹窗
 */
class RedPacketDialog(context: Context, themeResId: Int, private val listener: OnClickListener, val price:String):Dialog(context,themeResId) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_redpacket)
        initView()
    }
    private fun initView(){
        tvPrice.text=price
        ivBG.setOnClickListener {
            setAnimtor()
        }
        viewBut.setOnClickListener {
            listener.click(0,"")
        }
        viewBut.isClickable=false
        window!!.setWindowAnimations(R.style.scale_in_alpha_out)
        setCanceledOnTouchOutside(false)
        //设置dialog没有边距
        window!!.decorView.setPadding(0, 0, 0, 0)
        val lp = window!!.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = lp
        setCameraDistance()
    }
    private fun setAnimtor(){
        ivBG.isClickable=false
        val mAnimatorA = ObjectAnimator.ofFloat(ivBG, View.ROTATION_Y, 0f, 90f).setDuration(300)
        val mAnimatorB = ObjectAnimator.ofFloat(clBG, View.ROTATION_Y, 270f, 360f).setDuration(300)
        mAnimatorA.start()
        mAnimatorA.addListener(object :AnimatorListenerAdapter(){
            override fun onAnimationEnd(animation: Animator?) {
                mAnimatorB.start()
            }
        })
        mAnimatorB.addListener(object :AnimatorListenerAdapter(){
            override fun onAnimationEnd(animation: Animator?) {
                viewBut.isClickable=true
                setCanceledOnTouchOutside(true)
                openRedPacket()
            }
        })
    }
    private fun setCameraDistance() {
        val distance = 15000
        val scale = context.resources.displayMetrics.density * distance
        ivBG.cameraDistance = scale
        clBG.cameraDistance = scale
    }
    private fun openRedPacket(){
        val map = HashMap<String, String>()
        map["userId"]=AppUtils.getString(Contacts.USERID,"")
        RetrofitWorker.retrofitWorker.openRedPacket(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<String>>(){
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<String>) {
                        if (it.code==200){
                            AppUtils.setString("RedPacketStatus","true")
                            ToastUtil.getInstance().showToast(it.data?:"")
                        }
                    }

                    override fun onError(e: Throwable) {

                    }
                })
    }
}